/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * RSScale.cpp
 *
 *  Created on: 2010/03/05
 *      Author: Alexandre Pires ( c.alexandre.pires@gmail.com )
 */

#include "RSScale.h"
#include "RSSerializer.h"
#include <QXmlStreamWriter>

namespace Realsphere {

    RSScale::RSScale ( const QString& rScopeId ) :
        RSTransformationElement ( RS_SCALE, rScopeId ), mProportion ( 1, 1, 1 )
    {
    }

    RSScale::~RSScale ()
    {
    }

    QVector3D RSScale::Proportion () const
    {
        return mProportion;
    }

    void RSScale::SetProportion ( const QVector3D& rProportion, bool rEmit )
    {
        mProportion = rProportion;
        SetDirty ( true );

        if ( rEmit )
            emit ProportionChanged ( rProportion );
    }

    void RSScale::SetProportion ( qreal rXProportion, qreal rYProportion, qreal rZProportion, bool rEmit )
    {
        QVector3D proportion ( rXProportion, rYProportion, rZProportion );
        SetProportion ( proportion, rEmit );
    }

    void RSScale::ApplyScaling ( const QVector3D& rFactor )
    {
        SetProportion ( mProportion * rFactor );
    }

    void RSScale::ApplyScaling ( qreal rXProportion, qreal rYProportion, qreal rZProportion )
    {
        QVector3D factor ( rXProportion, rYProportion, rZProportion );
        ApplyScaling ( factor );
    }

    void RSScale::WriteElementData ( QXmlStreamWriter *rXmlWriter )
    {
        rXmlWriter->writeCharacters ( RSSerializer::serialize ( mProportion ) );
    }

    void RSScale::UpdateTransformation ()
    {
        QMatrix4x4 transformation;
        transformation.scale ( mProportion );
        SetTransformation ( transformation );
        SetDirty ( false );
    }
}
